/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */
package org.apache.directory.studio.persistence.view.wizards;


import org.apache.directory.studio.ldapbrowser.core.model.IEntry;
import org.apache.directory.studio.persistence.core.CodeGeneratorContext;
import org.apache.directory.studio.persistence.core.LdapEntryAnalyzer;
import org.apache.directory.studio.persistence.core.CodeGenerator;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;


public class NewPersistenceCodeWizard extends Wizard implements INewWizard
{

    private NewPersistenceCodeWizardPage codeWizardPage;
    private AddMembersWizardPage membersWizardPage;
    private IEntry entry;
    private CodeGeneratorContext codeGeneratorContext;
    private DaoRequiredParametersWizardPage requiredParametersWizardPage;


    /**
     * @return the entry
     */
    public IEntry getEntry()
    {
        return entry;
    }


    /**
     * @param entry the entry to set
     */
    public void setEntry( IEntry entry )
    {
        this.entry = entry;
    }


    @Override
    public boolean performFinish()
    {
        CodeGenerator codeGenerator = new CodeGenerator();
        codeGenerator.generateCode( codeGeneratorContext, "java_template.vm" );
        codeGenerator.generateGenericDaoCode( codeGeneratorContext, "genericLdapDao_template.vm" );
        codeGenerator.generateDaoCode( codeGeneratorContext, "dao_template.vm" );
        codeGenerator.generatePom( codeGeneratorContext, "pom.vm" );
        return true;
    }


    public void init( IWorkbench workbench, IStructuredSelection selection )
    {
        LdapEntryAnalyzer analyzer = new LdapEntryAnalyzer();
        codeGeneratorContext = analyzer.analyze( getEntry() );
        // Creating pages
        codeWizardPage = new NewPersistenceCodeWizardPage();
        membersWizardPage = new AddMembersWizardPage();
        requiredParametersWizardPage = new DaoRequiredParametersWizardPage();
        codeWizardPage.setEntryInformation( codeGeneratorContext );
        membersWizardPage.setEntryInformation( codeGeneratorContext );
        requiredParametersWizardPage.setCodeGenContext( codeGeneratorContext );
    }


    public void addPages()
    {
        // Adding pages
        addPage( codeWizardPage );
        addPage( membersWizardPage );
        addPage( requiredParametersWizardPage );
    }

}
